package com.flyco.modules.ums.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.util.BizUtil;
import com.flyco.common.util.HttpUtil;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.ums.entity.UmsMember;
import com.flyco.modules.ums.entity.UmsMemberRoleRecord;
import com.flyco.modules.ums.service.IUmsMemberRoleRecordService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.modules.ums.service.IUmsMemberService;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 用户角色记录表
 * @Author: flyco
 * @Date:   2022-05-17
 * @Version: V1.0
 */
@Slf4j
@Api(tags="用户角色记录表")
@RestController
@RequestMapping("/f/ums/umsMemberRoleRecord")
public class UmsMemberRoleRecordController extends BaseController<UmsMemberRoleRecord, IUmsMemberRoleRecordService> {
	@Autowired
	private IUmsMemberRoleRecordService umsMemberRoleRecordService;
	@Autowired
	private IUmsMemberService umsMemberService;
	/**
	 * 分页列表查询
	 *
	 * @param umsMemberRoleRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "用户角色记录表-分页列表查询")
	@ApiOperation(value="用户角色记录表-分页列表查询", notes="用户角色记录表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(UmsMemberRoleRecord umsMemberRoleRecord,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<UmsMemberRoleRecord> queryWrapper = QueryGenerator.initQueryWrapper(umsMemberRoleRecord, req.getParameterMap());
		Page<UmsMemberRoleRecord> page = new Page<UmsMemberRoleRecord>(pageNo, pageSize);
		IPage<UmsMemberRoleRecord> pageList = umsMemberRoleRecordService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param umsMemberRoleRecord
	 * @return
	 */
	@AutoLog(value = "用户角色记录表-添加")
	@ApiOperation(value="用户角色记录表-添加", notes="用户角色记录表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody UmsMemberRoleRecord umsMemberRoleRecord) {
		if (oConvertUtils.isEmpty(umsMemberRoleRecord)) return Result.error("数据为空！");
		if (oConvertUtils.isEmpty(umsMemberRoleRecord.getMemberId()) && oConvertUtils.isEmpty(umsMemberRoleRecord.getMobile())) return Result.error("用户编号与手机好不可都为空！");
		if (oConvertUtils.isEmpty(umsMemberRoleRecord.getMemberId())) {
			LambdaQueryWrapper<UmsMember> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(UmsMember::getBrandId,1);
			queryWrapper.eq(UmsMember::getType,1);
			queryWrapper.eq(UmsMember::getMobile, umsMemberRoleRecord.getMobile());
			UmsMember member = umsMemberService.getOne(queryWrapper, false);
			umsMemberRoleRecord.setMemberId(member.getId());
		}
		if (oConvertUtils.isEmpty(umsMemberRoleRecord.getMobile())) {
			UmsMember member = umsMemberService.getById(umsMemberRoleRecord.getMemberId());
			umsMemberRoleRecord.setMobile(member.getMobile());
		}
		UmsMember updateMember = new UmsMember();
		updateMember.setId(umsMemberRoleRecord.getMemberId());
		updateMember.setRole(umsMemberRoleRecord.getRole());
		umsMemberService.updateById(updateMember);
		umsMemberRoleRecordService.save(umsMemberRoleRecord);
		if (oConvertUtils.isNotEmpty(umsMemberRoleRecord.getMobile())) {
			refreshCache(umsMemberRoleRecord.getMobile());
		}
		return Result.ok("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param umsMemberRoleRecord
	 * @return
	 */
	@AutoLog(value = "用户角色记录表-编辑")
	@ApiOperation(value="用户角色记录表-编辑", notes="用户角色记录表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody UmsMemberRoleRecord umsMemberRoleRecord) {
		if (oConvertUtils.isEmpty(umsMemberRoleRecord)) return Result.error("数据为空！");
		UmsMember updateMember = new UmsMember();
		updateMember.setId(umsMemberRoleRecord.getMemberId());
		updateMember.setRole(umsMemberRoleRecord.getRole());
		umsMemberService.updateById(updateMember);
		umsMemberRoleRecordService.updateById(umsMemberRoleRecord);
		if (oConvertUtils.isNotEmpty(umsMemberRoleRecord.getMobile())) {
			refreshCache(umsMemberRoleRecord.getMobile());
		}
		return Result.ok("编辑成功!");
	}


	private void  refreshCache(String mobile) {
		try {
			StringBuilder url = new StringBuilder();
			url.append(BizUtil.getSsoUrl());
			url.append("/api/sso/update-inner-staff/");
			url.append(mobile);
			String result = HttpUtil.getRequest(url.toString());
			log.info("result" + result);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "用户角色记录表-通过id删除")
	@ApiOperation(value="用户角色记录表-通过id删除", notes="用户角色记录表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		umsMemberRoleRecordService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "用户角色记录表-批量删除")
	@ApiOperation(value="用户角色记录表-批量删除", notes="用户角色记录表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.umsMemberRoleRecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "用户角色记录表-通过id查询")
	@ApiOperation(value="用户角色记录表-通过id查询", notes="用户角色记录表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		UmsMemberRoleRecord umsMemberRoleRecord = umsMemberRoleRecordService.getById(id);
		return Result.ok(umsMemberRoleRecord);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param umsMemberRoleRecord
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, UmsMemberRoleRecord umsMemberRoleRecord) {
      return super.exportXls(request, umsMemberRoleRecord, UmsMemberRoleRecord.class, "用户角色记录表");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, UmsMemberRoleRecord.class);
  }

}
